package com.ai.vita.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class UserAgent {

    private eu.bitwalker.useragentutils.UserAgent ua;

    public static UserAgent parse(HttpServletRequest request) {
        return parse(request.getHeader("User-Agent"));
    }

    public static UserAgent parse(String userAgentString) {
        return new UserAgent(eu.bitwalker.useragentutils.UserAgent.parseUserAgentString(userAgentString));
    }

    public UserAgent(eu.bitwalker.useragentutils.UserAgent userAgent) {
        ua = userAgent;
    }

    /**
     * 浏览器版本
     * @return
     */
    public String getBrowserVersion(){
        return ua.getBrowserVersion().getVersion();
    }

    /**
     * 浏览器名称
     * @return
     */
    public String getBrowser(){
        return ua.getBrowser().getName();
    }

    /**
     * 浏览器类型
     * @return
     */
    public String getBrowserType(){
        return ua.getBrowser().getBrowserType().getName();
    }

    /**
     * 浏览器厂商
     * @return
     */
    public String getBrowserManufacturer(){
        return ua.getBrowser().getManufacturer().getName();
    }

    /**
     * 浏览器内核
     * @return
     */
    public String getBrowserRenderingEngine(){
        return ua.getBrowser().getRenderingEngine().name();
    }

    /**
     * 操作系统
     * @return
     */
    public String getOperatingSystem(){
        return ua.getOperatingSystem().getName();
    }

    /**
     * 操作系统厂商
     * @return
     */
    public String getOperatingSystemManufacturer(){
        return ua.getOperatingSystem().getManufacturer().getName();
    }

    /**
     * 设备类型
     * @return
     */
    public String getDeviceType(){
        return ua.getOperatingSystem().getDeviceType().getName();
    }

    public Map<String, String> getGroup(){
        Map<String, String> re = new HashMap<String, String>();
        re.put("browserVersion", this.getBrowserVersion());
        re.put("browser", this.getBrowser());
        re.put("browserType", this.getBrowserType());
        re.put("browserManufacturer", this.getBrowserManufacturer());
        re.put("browserRenderingEngine", this.getBrowserRenderingEngine());
        re.put("operatingSystem", this.getOperatingSystem());
        re.put("operatingSystemManufacturer", this.getOperatingSystemManufacturer());
        re.put("deviceType", this.getDeviceType());
        return re;
    }

}
